package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.MailUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.domain.MailJob;
import com.ruoyi.quartz.mapper.MailJobMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private ISysUserService sysUserService;

    @Resource
    private MailJobMapper mailJobMapper;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    /**
     * 定时任务发送邮件
     */
    public void sendMail(){
        // 获取所有代办信息
        List<MailJob> mailJobList = mailJobMapper.queryAllUnDoneTask();

        // 整合数据,同一个员工要发送的邮件
        Map<String, List<MailJob>> mail = new HashMap<>();

        for(MailJob mailJob : mailJobList){
            if(!StringUtils.isEmpty(mailJob.getUserName())){

                List<MailJob> mailList = mail.get(mailJob.getUserName());
                if(mailList == null){
                    mailList = new ArrayList<>();
                    mail.put(mailJob.getUserName(), mailList);
                }
                mailList.add(mailJob);
            }
        }

        // 设置每个人的邮件内容

        for (Map.Entry<String, List<MailJob>> entry : mail.entrySet()) {
            String email = "";
            StringBuilder content = new StringBuilder("您好：<br/>您有如下待办未处理，请尽快登录审批平台处理。<br/>列表：<br/>");
            content.append("<table border='1' cellspacing='0'><tr><td align='center'>申请人</td><td align='center'>详情</td></tr>");
            for(MailJob mailJob : entry.getValue()){
                email = mailJob.getUserEmail();
                content.append("<tr>");
                content.append("<td>");
                content.append(mailJob.getNickName());
                content.append("</td>");
                content.append("<td>");
                content.append(mailJob.getDetailInfo());
                content.append("</td>");
                content.append("</tr>");
            }
            content.append("</table>");
            MailUtils.sendMail("【审批平台】待办提醒", content.toString(), new String[]{email});
        }
    }
}
